from django.utils.datetime_safe import datetime
from django.http import HttpResponse, HttpResponseRedirect
from willconta.transfers.models import Movimiento
from django.shortcuts import render_to_response
from willconta.contact.forms import ContactForm
from django.core.mail import send_mail
from django.forms.models import modelformset_factory
from django.contrib.auth.decorators import login_required
from willconta.transfers.forms import MovimientoForm

def main(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

def get_movimientos (request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

@login_required(redirect_field_name='/accounts/login/')
def create_mov (request):
    user = request.user
    if request.method == 'POST':
        form = MovimientoForm(request.POST)
        movimiento = form.save(commit=False)
        movimiento.type = "Real"
        movimiento.save()
        return render_to_response("/movimientos/", locals())
    else:
        return render_to_response("edit_movimiento.html", locals())
#    user = request.user
#    MovimientoFormSet = modelformset_factory(Movimiento)
#    if request.method == 'POST':
#        formset = MovimientoFormSet(request.POST, request.FILES)
#        if formset.is_valid():
#            """
#            Crear recoger la fecha que va en el formulario y en funcion 
#            de ella meter si es una prevision o una transferencia 
#            """
#            formset.save()
#            return render_to_response("/movimientos/", locals())
#    else:
#        formset = MovimientoFormSet()
#    return render_to_response("edit_movimiento.html", locals())

#para mostrar informacion que  va en el meta 
def display_meta(request):
    values = request.META.items()
    values.sort()
    html = []
    for k, v in values:
        html.append('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
    return HttpResponse('<table>%s</table>' % '\n'.join(html))

def search(request):
    current_section = 'Search'
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        movimientos = Movimiento.objects.filter(asunto__icontains=q)
        return render_to_response('search_results.html',locals())
    else:
        return render_to_response('search_results.html',{'error': False})

def thanks (request):
    return HttpResponse('gracias')

def contact(request):
    current_section = 'Contacto'
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['asunto'],
                cd['mensaje'],
                cd.get('email', 'noreply@example.com'),
                ['carlosvin@gmail.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form =  ContactForm()
    return render_to_response('contact.html', {'form': ContactForm(), 'current_section': current_section})